local matRefraction	= matRefraction = Material( "effects/strider_pinch_dudv" ) --Material( "refract_ring" )

local tMats = {}
tMats.Glow1 = Material("sprites/light_glow02")
tMats.Glow2 = Material("sprites/flare1")

for _,mat in pairs(tMats) do

	mat:SetMaterialInt("$spriterendermode",9)
	mat:SetMaterialInt("$ignorez",1)
	mat:SetMaterialInt("$illumfactor",8)
	
end

local SmokeParticleUpdate = function(particle)

	if particle:GetStartAlpha() == 0 and particle:GetLifeTime() >= 0.5*particle:GetDieTime() then
		particle:SetStartAlpha(particle:GetEndAlpha())
		particle:SetEndAlpha(0)
		particle:SetNextThink(-1)
	else
		particle:SetNextThink(CurTime() + 0.1)
	end

	return particle

end


function EFFECT:Init(data)

	self.Scale = data:GetScale()
	self.ScaleSlow = math.sqrt(self.Scale)
	self.ScaleSlowest = math.sqrt(self.ScaleSlow)
	self.Normal = data:GetNormal()
	self.RightAngle = self.Normal:Angle():Right():Angle()
	self.Position = data:GetOrigin() - 12*self.Normal
	self.Position2 = self.Position + self.Scale*64*self.Normal

	local CurrentTime = CurTime()
	self.Duration = 0.5*self.Scale 
	self.KillTime = CurrentTime + self.Duration
	self.GlowAlpha = 200
	self.GlowSize = 100*self.Scale
	self.FlashAlpha = 100
	self.FlashSize = 0

	self.Refract = 0.5
	self.DeltaRefract = 0.06
	self.Size = 0
	self.MaxSize = 4e5
	if render.GetDXLevel() <= 81 then
		matRefraction = Material( "effects/strider_pinch_dudv" )
		self.Refract = 0.3
		self.DeltaRefract = 0.03
		self.MaxSize = 2e5
	end


	local emitter = ParticleEmitter(self.Position)

			


		





	--fire burst2
		for i=1,math.ceil(self.Scale*25) do
			
			local vecang = (self.Normal + VectorRand()*math.Rand(0.5,1.5)):GetNormalized()
			local velocity = math.Rand(750,750)*vecang*self.Scale
			local particle = emitter:Add("effects/fire_cloud"..math.random(1,2),  self.Position - vecang*32*self.Scale)
			particle:SetVelocity(velocity)
			particle:SetDieTime(math.Rand(2,2))
			particle:SetStartAlpha(math.Rand(230,250))
			particle:SetStartSize(math.Rand(100,100)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(100,100)*self.ScaleSlow)
			particle:SetStartLength(math.Rand(500,500)*self.ScaleSlow)
			particle:SetEndLength(math.Rand(500,500)*self.ScaleSlow)
			particle:SetColor(0,0,255)

		end


	--fire burst1
		for i=1,math.ceil(self.Scale*25) do
			
			local vecang = (self.Normal + VectorRand()*math.Rand(0.5,1.5)):GetNormalized()
			local velocity = math.Rand(750,750)*vecang*self.Scale
			local particle = emitter:Add("effects/fire_cloud"..math.random(1,2),  self.Position - vecang*32*self.Scale)
			particle:SetVelocity(velocity)
			particle:SetDieTime(math.Rand(2,2))
			particle:SetStartAlpha(math.Rand(230,250))
			particle:SetStartSize(math.Rand(100,100)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(100,100)*self.ScaleSlow)
			particle:SetStartLength(math.Rand(500,500)*self.ScaleSlow)
			particle:SetEndLength(math.Rand(500,500)*self.ScaleSlow)
			particle:SetColor(255,255,255)

		end

--pillar of dust
		for i=1,math.ceil(self.Scale*12) do

			local vecang = (self.Normal + VectorRand()*math.Rand(0,0.5)):GetNormalized()
			local velocity = math.Rand(-300,500)*vecang*self.Scale
			local particle = emitter:Add("particles/mat1",  self.Position + vecang*math.Rand(-20,25)*self.Scale)
			local dietime = math.Rand(5.5,5.9)*self.Scale
			particle:SetVelocity(velocity)
			particle:SetGravity(Vector(0,0,math.Rand(50,250)))
			particle:SetAirResistance(65)
			particle:SetDieTime(dietime)
			particle:SetLifeTime(math.Rand(-0.12,-0.08))
			particle:SetStartAlpha(200)
			particle:SetEndAlpha(0)
			particle:SetThinkFunction(SmokeParticleUpdate)
			particle:SetNextThink(CurrentTime + 0.5*dietime)
			particle:SetStartSize(math.Rand(90,140)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(240,280)*self.ScaleSlow)
			particle:SetRoll(math.Rand(150,170))
			particle:SetRollDelta(0.7*math.random(-1,1))
			particle:SetColor(1,1,1)
			
		end
	--dust puff
		for i=1,math.ceil(self.Scale*57) do

			local vecang = self.RightAngle
			vecang:RotateAroundAxis(self.Normal,math.Rand(0,360))
			vecang = vecang:Forward() + VectorRand()*0.1
			local velocity = math.Rand(100,300)*vecang
			local particle = emitter:Add("particles/mat1",  self.Position - vecang*32*self.Scale - self.Normal*16)
			local dietime = math.Rand(5.5,5.9)*self.Scale
			particle:SetVelocity(velocity*self.Scale)
			particle:SetGravity(Vector(0,0,math.Rand(0,0)))
			particle:SetAirResistance(50)
			particle:SetDieTime(dietime)
			particle:SetLifeTime(math.Rand(-0.12,-0.08))
			particle:SetStartAlpha(200)
			particle:SetEndAlpha(0)
			particle:SetThinkFunction(SmokeParticleUpdate)
			particle:SetNextThink(CurrentTime + 0.5*dietime)
			particle:SetStartSize(math.Rand(90,110)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(130,150)*self.ScaleSlow)
			particle:SetRoll(math.Rand(150,180))
			particle:SetRollDelta(0.6*math.random(-1,1))
			particle:SetColor(1,1,1)
			
		end

		--dust puffSmall
		for i=1,math.ceil(self.Scale*120) do

			local vecang = self.RightAngle
			vecang:RotateAroundAxis(self.Normal,math.Rand(0,712))
			vecang = vecang:Forward() + VectorRand()*0.1
			local velocity = math.Rand(100,300)*vecang
			local particle = emitter:Add("particles/mat1",  self.Position - vecang*32*self.Scale - self.Normal*16)
			local dietime = math.Rand(5.5,5.9)*self.Scale
			particle:SetVelocity(velocity*self.Scale)
			particle:SetGravity(Vector(0,0,math.Rand(0,0)))
			particle:SetAirResistance(50)
			particle:SetDieTime(dietime)
			particle:SetLifeTime(math.Rand(-0.12,-0.08))
			particle:SetStartAlpha(200)
			particle:SetEndAlpha(0)
			particle:SetThinkFunction(SmokeParticleUpdate)
			particle:SetNextThink(CurrentTime + 0.5*dietime)
			particle:SetStartSize(math.Rand(25,25)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(25,25)*self.ScaleSlow)
			particle:SetRoll(math.Rand(150,180))
			particle:SetRollDelta(0.6*math.random(-1,1))
			particle:SetColor(1,1,1)
			
		end
	--dust cloud
		for i=1,math.ceil(self.Scale*10) do

			local vecang = self.RightAngle			
                  vecang:RotateAroundAxis(self.Normal,math.Rand(0,500))
			vecang = vecang:Forward() + VectorRand()*0.1
			local velocity = math.Rand(150,300)*vecang*self.Scale
			local particle = emitter:Add("particles/mat1",  self.Position + vecang*math.Rand(50,50)*self.Scale)
			local dietime = math.Rand(5.5,5.9)*self.Scale
			particle:SetVelocity(velocity)
			particle:SetGravity(Vector(0,0,math.Rand(500,500)))
			particle:SetAirResistance(65)
			particle:SetDieTime(dietime)
			particle:SetLifeTime(math.Rand(-0.2,-0.15))
			particle:SetStartAlpha(200)
			particle:SetEndAlpha(0)
			particle:SetThinkFunction(SmokeParticleUpdate)
			particle:SetNextThink(CurrentTime + 0.5*dietime)
			particle:SetStartSize(math.Rand(500,500)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(500,500)*self.ScaleSlow)
			particle:SetRoll(math.Rand(180,200))
			particle:SetRollDelta(0.85*math.random(-1,1))
			particle:SetColor(1,1,1)
			
		end


		--fire ball2
		for i=1,math.ceil(self.Scale*24) do
			
			local vecang = (self.Normal + VectorRand()*math.Rand(1,1)):GetNormalized()
			local velocity = math.Rand(100,200)*vecang*self.Scale
			local particle = emitter:Add("effects/fire_cloud"..math.random(1,2), self.Position + vecang*math.Rand(0,70)*self.Scale)
			particle:SetVelocity(velocity)
			particle:SetGravity(VectorRand()*math.Rand(200,400) + Vector(0,0,math.Rand(500,700)))
			particle:SetAirResistance(250)
			particle:SetDieTime(math.Rand(1,1)*self.Scale)
			particle:SetStartAlpha(math.Rand(230,250))
			particle:SetStartSize(math.Rand(310,340)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(350,390)*self.ScaleSlow)
			particle:SetRoll(math.Rand(150,180))
			particle:SetRollDelta(0.6*math.random(-1,1))
			particle:SetColor(255,255,255)

		end	


	--fire puff4
		for i=1,math.ceil(self.Scale*25) do
		
			local vecang = (self.Normal + VectorRand()*math.Rand(0,360)):GetNormalized()
			local velocity = math.Rand(256,200)*vecang
			local particle = emitter:Add("effects/fire_cloud"..math.random(1,2), self.Position + vecang*16*self.Scale)
			particle:SetVelocity(velocity*self.Scale)
			particle:SetGravity(VectorRand()*math.Rand(200,400))
			particle:SetAirResistance(300)
			particle:SetDieTime(math.Rand(1,1)*self.Scale)
			particle:SetStartAlpha(math.Rand(230,250))
			particle:SetStartSize(math.Rand(100,130)*self.ScaleSlow)
			particle:SetEndSize(math.Rand(140,180)*self.ScaleSlow)
			particle:SetRoll(math.Rand(150,180))
			particle:SetRollDelta(0.4*math.random(-1,1))
			particle:SetColor(255,255,255)
			
		end

		if self.Scale > 1 then
		
		--shock wave
			for i=1,math.ceil(self.Scale*20) do

				local vecang = Vector(math.Rand(-1,1),math.Rand(-1,1),0):GetNormalized()
				local velocity = math.Rand(1000,1000)*vecang
				local particle = emitter:Add("sprites/heatwave", self.Position - vecang*64*self.Scale - Vector(0,0,80*self.ScaleSlowest))
				local dietime = 0.08*self.Scale
				particle:SetVelocity(velocity)
				particle:SetDieTime(dietime)
				particle:SetLifeTime(0)
				particle:SetStartAlpha(60)
				particle:SetEndAlpha(0)
				particle:SetStartSize(160*self.ScaleSlowest)
				particle:SetEndSize(200*self.ScaleSlowest)
				particle:SetColor(255,255,255)
				
			end
	
		end

	emitter:Finish()
	
	
end


--THINK
-- Returning false makes the entity die
function EFFECT:Think()
	local TimeLeft = self.KillTime - CurTime()
	local TimeScale = TimeLeft/self.Duration
	local FTime = FrameTime()
	if TimeLeft > 0 then 

		self.FlashAlpha = self.FlashAlpha - 200*FTime
		self.FlashSize = self.FlashSize + 60000*FTime
		
		self.GlowAlpha = 200*TimeScale
		self.GlowSize = TimeLeft*self.Scale

		return true
	else
		return false	
	end

	self.Refract = self.Refract + 2.0 * FrameTime()
	self.Size = 512 * self.Refract^(0.2)
	
	if ( self.Refract >= 10 ) then return false end
	
	return true

end

-- Draw the effect
function EFFECT:Render()
local startpos = self.Position


--base glow
render.SetMaterial(tMats.Glow1)
render.DrawSprite(self.Position2,7000*self.GlowSize,5500*self.GlowSize,Color(255,240,220,self.GlowAlpha))

--blinding flash
	if self.FlashAlpha > 0 then
		render.SetMaterial(tMats.Glow2)
		render.DrawSprite(self.Position2,self.FlashSize,self.FlashSize,Color(255,245,215,self.FlashAlpha))

end



--shockwave2
if self.Scale > 1 then
		
		matRefraction:SetMaterialFloat( "$refractamount", math.sin(self.Refract*math.pi) * 2.0 )
		render.SetMaterial( matRefraction )
		render.UpdateRefractTexture()
		
		render.DrawQuadEasy( startpos,
		Vector(0,0,1),
		self.Scale, self.Scale)
		
	end


end







